<?php
/**
 * Created by PhpStorm.
 * User: Lvyj
 * Date: 2017/6/21 0021
 * Time: 17:14
 */

namespace Admin\Assist\Controllers;

use Helpers\Main;
use Helpers\Tool;
use Admin\Assist\Service\AssistService as Assist;

class ServiceController extends ControllerBase
{
    /**
     * 搜索获取指令分页列表
     */
    public function getInstructionListAction()
    {
        $this->view->disable();
        $page = $this->request->getPost('page', 'int', 1);
        $rows = $this->request->getPost('rows', 'int', 10);
        $keywords = $this->request->getPost('keywords', 'string', '');
        //调用assist服务
        $data = Assist::getInstructionList($page, $rows, $keywords);
        return $this->response->setJsonContent($data);
    }

    /**
     * 创建指令
     */
    public function addInstructionAction()
    {
        $this->view->disable();
        $cont = $this->request->getPost('content', 'string');
        $content = htmlspecialchars_decode($cont);
        $name = $this->request->getPost('name', 'string');
        $description = $this->request->getPost('description', 'string');
        $type = intval($this->request->getPost('type', 'string'));
        //调用assist服务
        $data = Assist::addInstruction($content, $name, $description, $type);
        return $this->response->setContent($data);
    }

    /**
     * 删除指令
     */
    public function delInstructionAction()
    {
        $this->view->disable();
        $idStr = $this->request->getPost('ids', 'string');
        $idStr or die('缺少删除id！');
        $data = Assist::delInstruction($idStr);
        return $this->response->setContent($data);
    }

    /**
     * 搜索分页获取协助列表
     */
    public function getAssistanceListAction()
    {
        $this->view->disable();
        $page = $this->request->getPost('page', 'int', 1);
        $rows = $this->request->getPost('rows', 'int', 10);
        $keywords = $this->request->getPost('keywords', 'string', '');
        //调用assist服务
        $data = Assist::getAssistanceList($page, $rows, $keywords);
        return $this->response->setJsonContent($data);
    }

    /**
     * 分页搜索用户
     */

    public function getUserListAction()
    {
        $this->view->disable();
        $page = $this->request->getPost('page', 'int', 1) - 1;
        $rows = $this->request->getPost('rows', 'int', 10);
        $keywords = $this->request->getPost('keywords', 'string', '');
        //调用user服务
        $data = Assist::searchUser($page, $rows, $keywords);
        return $this->response->setJsonContent($data);
    }

    /**
     * 获取用户绑定设备列表
     */
    public function getUserDevicesAction()
    {
        $this->view->disable();
        $id = $this->request->get('id', "int");
        $data = Assist::getUserDevices($id);
        return $this->response->setJsonContent($data);
    }

    public function getUserClientsAction()
    {
        $this->view->disable();
        $id = $this->request->get('id', 'int');
        $data = Assist::getUserClients($id);
        return $this->response->setJsonContent($data);
    }

    /**
     * 获取协助详情
     */
    public function getAssistDetailAction()
    {
        $this->view->disable();
        $assistanceId = $this->request->get('id', 'string');
        $data = Assist::getAssistDetail($assistanceId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 获取任务详情
     */
    public function getTaskDetailAction()
    {
        $this->view->disable();
        $assistId = $this->request->get('assistId', 'string');
        $taskId = $this->request->get('taskId', 'string');
        $data = Assist::getTaskDetail($assistId, $taskId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 创建协助 服务端发起协助
     */
    public function createAssistAction()
    {
        $this->view->disable();
        $deviceId = $this->request->get('deviceId', 'string');
        $userId = $this->request->get('userId', 'int');
        $userName = $this->request->get('userName', 'string');
        $data = Assist::createAssist($deviceId, $userId, $userName);
        return $this->response->setJsonContent($data);
    }

    /**
     * 发布任务
     */
    public function distributeTaskAction()
    {
        $this->view->disable();
        $assistId = $this->request->getPost('id', 'string');
        $type = $this->request->getPost('type', 'int');
        $instruction = $this->request->getPost('content', 'string');
        $data = Assist::distributeTask($assistId, $type, $instruction);

        return $this->response->setJsonContent($data);
    }

    /**
     * 退出协助
     */
    public function quitAssistanceAction()
    {
        $this->view->disable();
        $assistId = $this->request->get('assistId', 'string');
        $data = Assist::quitAssistance($assistId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 客户端协助请求列表
     */
    public function getClientExecutingAssistsAction()
    {
        $this->view->disable();
        $page = $this->request->getPost('page', 'int', 1);
        $rows = $this->request->getPost('rows', 'int', 10);
        $keywords = $this->request->getPost('keywords', 'string', '');
        //调用assist服务
        $data = Assist::getAssistRequest($page, $rows, $keywords);
        return $this->response->setJsonContent($data);
    }

    /**
     * 同意客户端协助请求
     */
    public function agreeAssistanceAction()
    {
        $this->view->disable();
        $assistId = $this->request->get('id', 'string');
        $data = Assist::agreeAssistRequest($assistId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 协助存活
     */
    public function getAssistAliveAction()
    {
        $this->view->disable();
        $assistId = $this->request->get('assistId', 'string');
        $data = Assist::getAssistAlive($assistId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 获取目录结构
     */
    public function getAssistCatalogAction()
    {
        $this->view->disable();
        $assistId = $this->request->get('assistId', 'string');
        $taskId = $this->request->get('taskId', 'string');
        $data = Assist::getCatalogs($assistId, $taskId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 获取任务状态
     */
    public function getAssistCatalogInfoAction()
    {
        $this->view->disable();
        $assistId = $this->request->get('assistId', 'string');
        $taskId = $this->request->get('taskId', 'string');
        $data = Assist::getAssistCatalogInfo($assistId, $taskId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 获取任务信息
     */
    public function getTaskInfoAction()
    {
        $this->view->disable();
        $assistId = $this->request->get('assistId', 'string');
        $taskId = $this->request->get('taskId', 'string');
        $data = Assist::getTaskInfo($assistId, $taskId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 获取安装应用列表
     */
    public function getAppsAction()
    {
        $this->view->disable();
        $assistId = $this->request->get('assistId', 'string');
        $taskId = $this->request->get('taskId', 'string');
        $data = Assist::getInstalledApps($assistId, $taskId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 获取网络配置
     *
     */
    public function getNetworkAction()
    {
        $this->view->disable();
        $assistId = $this->request->get('assistId', 'string');
        $taskId = $this->request->get('taskId', 'string');
        $data = Assist::getNetwork($assistId, $taskId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 获取系统配置
     */
    public function getSystemAction()
    {
        $this->view->disable();
        $assistId = $this->request->get('assistId', 'string');
        $taskId = $this->request->get('taskId', 'string');
        $data = Assist::getSystem($assistId, $taskId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 获取报表列表
     */
    public function getReportsAction()
    {
        $this->view->disable();
        $page = $this->request->getPost('page', 'int', 1);
        $rows = $this->request->getPost('rows', 'int', 10);
        $type=$this->request->getPost('type', 'int', 0);
        $userId=$this->request->getPost('userId', 'string', null);
        $deviceId=$this->request->getPost('deviceId', 'string', null);
        $data = Assist::getReports($page, $rows, $type, $userId, $deviceId);
        return $this->response->setJsonContent($data);
    }

    /**
     * 上传文件成功
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20180131
     */
    public function uploadFileAction()
    {
        set_time_limit(0);
        if (!$this->request->hasFiles()) {
            return $this->sendErrorResponse(400, '缺少文件');
        }
        $file = $this->request->getUploadedFiles()[0];
        $filePath = Tool::buildPath(Main::getConfigByName('fileSave')['temp'], $file->getName());
        $file->moveTo($filePath);
        $url = Main::uploadToFileManageSystem($filePath, 'other', 'SB103014');
        if (!$url) {
            return $this->sendErrorResponse(500, '文件上传失败');
        }
        return $this->sendSuccessResponse($url);
    }

}